package com.sgb.jicai.view.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.jicai.R;
import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.databinding.ItemCentralizedMining3Binding;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.swgk.core.BR;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * Time:2021/5/8
 * AUTHOR:F_Q
 * Description:
 */
public class CentralizedMiningCuttingLineAdapter extends BaseRecycleAdapter<CentralizedMiningEntity> {
    private Context context;
    private boolean isVisible; // 是否显示进入商店入口

    public CentralizedMiningCuttingLineAdapter(Context context, List<CentralizedMiningEntity> datas) {
        super(context, R.layout.item_centralized_mining3, datas);
        this.context = context;
        isVisible = true;
    }

    @Override
    public void convert(ItemViewHolder holder, final CentralizedMiningEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ((ItemCentralizedMining3Binding) holder.getmBinding()).ent.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        ((ItemCentralizedMining3Binding) holder.getmBinding()).ent.setOnClickListener(view ->
                        JiCaiBaseApi.startWebSiteInfo( entity.getCompanyNo(), 1));
                //ShopInfoForCompanyTradesActivity.startAct());
        updateRecycler(((ItemCentralizedMining3Binding) holder.getmBinding()).childRecycler, entity);
        updateImageStatus(((ItemCentralizedMining3Binding) holder.getmBinding()).imgTab, entity);
        updateParamName(((ItemCentralizedMining3Binding) holder.getmBinding()).tvParamName, ((ItemCentralizedMining3Binding) holder.getmBinding()).tvFloorPrice, entity);
        updateGroupBuyType(((ItemCentralizedMining3Binding) holder.getmBinding()).tvStatus, entity);
        updateSecondlyType(((ItemCentralizedMining3Binding) holder.getmBinding()).tvSecondlyType, entity);
        updateQuotationType(((ItemCentralizedMining3Binding) holder.getmBinding()).tvQuotationType, entity);
        updateJoinType(((ItemCentralizedMining3Binding) holder.getmBinding()).tvJoinType, entity);
        if (entity.getGroupBuyMode().equals("1")){
            ((ItemCentralizedMining3Binding) holder.getmBinding()).tvJoinType.setVisibility(View.GONE);
        }else {
            ((ItemCentralizedMining3Binding) holder.getmBinding()).tvJoinType.setVisibility(View.VISIBLE);
        }

    }


    private void updateRecycler(RecyclerView recyclerView, CentralizedMiningEntity entity) {
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        CentralizedMiningChildAdapter childAdapter = new CentralizedMiningChildAdapter(recyclerView.getContext(), entity.getGoodsEntity());
        recyclerView.setAdapter(childAdapter);
        childAdapter.setOnItemClick(new AdapterOnItemClick<CentralizedMiningEntity.GoodsEntity>() {
            @Override
            public void onItemClick(CentralizedMiningEntity.GoodsEntity goodsEntity, int position) {
                if(onItemClick != null){
                    onItemClick.onItemClick(entity, position);
                }
            }
        });
    }

    private void updateImageStatus(ImageView imageView, CentralizedMiningEntity entity) {
        // 集采状态（1 集采中 2 集采结束 4 集采失败 5 加盟中 6 报价中 ）
        switch (entity.getStatus()) {
            case 1:
                switch (entity.getPriceWarning()) {//价格预警 1 大于 2 等于 3 小于
                    case 1:
                        imageView.setImageResource(R.drawable.jcz_dy);
                        break;
                    case 2:
                        imageView.setImageResource(R.drawable.jcz_dengyu);
                        break;
                    case 3:
                        imageView.setImageResource(R.drawable.jcz_xy);
                        break;
                    default:
                        imageView.setImageResource(R.drawable.jcz_dengyu);
                        break;
                }
                break;
            case 2:
                imageView.setImageResource(R.drawable.icon_jcwc);
                break;
            case 4:
                imageView.setImageResource(R.drawable.collect_fail);
                break;
            case 5:
                imageView.setImageResource(R.drawable.jmz_qt);
                break;
            case 6:
                switch (entity.getPriceWarning()) {//价格预警 1 大于 2 等于 3 小于
                    case 1:
                        imageView.setImageResource(R.drawable.bjz_dy);
                        break;
                    case 2:
                        imageView.setImageResource(R.drawable.bjz_dengyu);
                        break;
                    case 3:
                        imageView.setImageResource(R.drawable.bjz_xy);
                        break;
                }
//                imageView.setImageResource(R.drawable.icon_bjz);
                break;
            default:
                imageView.setImageResource(R.drawable.icon_jcz);
                break;
        }
    }

    private void updateParamName(TextView tvParamName, TextView tvFloorPrice, CentralizedMiningEntity entity) {
//        tvFloorPrice.setText(entity.getUnitName());
        // 集采分类 1商品集采 2 机械集采 3 设备集采 4 劳务集采 5房产集采 6租赁集采
//        switch (entity.getGroupBuyClass()) {
//            case 1:
//            default:
////                tvParamName.setText(tvParamName.getResources().getString(R.string.cm_commodity_param));
//                tvFloorPrice.setText(tvFloorPrice.getResources().getString(R.string.cm_min_price));
//                break;
//            case 2:
////                tvParamName.setText(tvParamName.getResources().getString(R.string.cm_machinery_param));
//                tvFloorPrice.setText(tvFloorPrice.getResources().getString(R.string.cm_min_price));
//                break;
//            case 3:
////                tvParamName.setText(tvParamName.getResources().getString(R.string.cm_equipment_param));
//                tvFloorPrice.setText(tvFloorPrice.getResources().getString(R.string.cm_min_price));
//                break;
//            case 4:
////                tvParamName.setText(tvParamName.getResources().getString(R.string.cm_labour_param));
//                tvFloorPrice.setText(tvFloorPrice.getResources().getString(R.string.cm_labour_min_price));
//                break;
//        }
    }

    private void updateGroupBuyType(TextView textView, CentralizedMiningEntity entity) {
        switch (entity.getGroupBuyType()) {//集采类型: 1即报集采 2 汇总集采  3.企业专项
            case 1:
                textView.setText(textView.getResources().getText(R.string.jbjc));
                textView.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            case 2:
                textView.setText(textView.getResources().getText(R.string.hzcg));
                textView.setBackgroundResource(R.drawable.shape_yellow_button);
                break;
            case 3:
                switch (entity.getOfferType()) {//报价类型: 0：市场报价   1：会员报价
                    case "1":
                        textView.setBackgroundResource(R.drawable.hybg);
                        textView.setText("客户报价");
                        break;
                    case "2":
                        textView.setBackgroundResource(R.drawable.shape_bule_green_button);
                        textView.setText("市场报价");
                        break;
                    case "3":
                        textView.setBackgroundResource(R.drawable.shape_bule_green_button);
                        textView.setText("成员报价");
                        break;
                }
                break;
            default:
                textView.setVisibility(View.GONE);
                break;
        }
    }
    //第二类型标签     //报价类型: 1：商品材料   2：机械租赁  3：设备租赁
    private void updateSecondlyType(TextView tvSecondlyType, CentralizedMiningEntity entity) {
        switch (entity.getGroupBuyClass()) {//报价类型: 0：市场报价   1：会员报价
            case 1:
                tvSecondlyType.setVisibility(View.VISIBLE);
                tvSecondlyType.setText("商品材料");
                tvSecondlyType.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            case 2:
                tvSecondlyType.setVisibility(View.VISIBLE);
                tvSecondlyType.setText("机械租赁");
                tvSecondlyType.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            case 3:
                tvSecondlyType.setVisibility(View.VISIBLE);
                tvSecondlyType.setText("设备租赁");
                tvSecondlyType.setBackgroundResource(R.drawable.shape_blue_button);
                break;
            default:
                tvSecondlyType.setVisibility(View.GONE);
                break;
        }
    }

    //报价类型标签
    private void updateQuotationType(TextView tvQuotationType, CentralizedMiningEntity entity) {
        if(entity.getOfferType()==null){
            tvQuotationType.setVisibility(View.GONE);
            return;
        }
        switch (entity.getOfferType()) {//报价类型: 0：市场报价   1：会员报价
            case "1":
                tvQuotationType.setVisibility(View.VISIBLE);
                tvQuotationType.setText("客户报价");
                tvQuotationType.setTextColor(context.getResources().getColor(R.color.color_FDA72D));
                tvQuotationType.setBackgroundResource(R.drawable.bg_market_join_price);
                break;
            case "2":
                tvQuotationType.setVisibility(View.VISIBLE);
                tvQuotationType.setTextColor(context.getResources().getColor(R.color.color_0286DF));
                tvQuotationType.setText("市场报价");
                tvQuotationType.setBackgroundResource(R.drawable.bg_marketprice);
                break;
            case "3":
                tvQuotationType.setVisibility(View.VISIBLE);
                tvQuotationType.setTextColor(context.getResources().getColor(R.color.color_FF4B10));
                tvQuotationType.setText("成员报价");
                tvQuotationType.setBackgroundResource(R.drawable.bg_memberoffer);
                break;
            default:
                tvQuotationType.setVisibility(View.GONE);
                break;
        }
    }

    //加盟客户类型
    private void updateJoinType(TextView tvJoinType, CentralizedMiningEntity entity) {
        if(entity.getJoinType()==null){
            if (entity.getGroupBuyMode().equals("2")){
                tvJoinType.setVisibility(View.VISIBLE);
                tvJoinType.setTextColor(context.getResources().getColor(R.color.color_FDA72D));
                tvJoinType.setText("客户加盟");
                tvJoinType.setBackgroundResource(R.drawable.bg_market_join_price);
            }else {
                tvJoinType.setVisibility(View.GONE);
            }
            return;
        }
        switch (entity.getJoinType()) {//1、市场加盟 2、会员加盟 3、成员加盟
            case "1":
                tvJoinType.setVisibility(View.VISIBLE);
                tvJoinType.setTextColor(context.getResources().getColor(R.color.color_FDA72D));
                tvJoinType.setText("客户加盟");
                tvJoinType.setBackgroundResource(R.drawable.bg_market_join_price);
                break;
            case "2":
                tvJoinType.setVisibility(View.VISIBLE);
                tvJoinType.setText("市场加盟");
                tvJoinType.setTextColor(context.getResources().getColor(R.color.color_0286DF));
                tvJoinType.setBackgroundResource(R.drawable.bg_marketprice);
                break;
            case "3":
                tvJoinType.setVisibility(View.VISIBLE);
                tvJoinType.setTextColor(context.getResources().getColor(R.color.color_FF4B10));
                tvJoinType.setText("成员加盟");
                tvJoinType.setBackgroundResource(R.drawable.bg_memberoffer);
                break;
            default:
                tvJoinType.setVisibility(View.GONE);
                break;
        }
    }
    public void setVisible(boolean visible) {
        isVisible = visible;
    }
}
